package com.tinusgraglin.enclusiv

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import android.util.Patterns
import androidx.activity.result.contract.ActivityResultContract
import com.tinusgraglin.enclusiv.datalayer.EditedArticle
import com.tinusgraglin.enclusiv.datalayer.ShowedArticleProfile
import com.tinusgraglin.enclusiv.datalayer.UserArticleStats
import com.tinusgraglin.enclusiv.ui.screens.ArticleActivity
import com.tinusgraglin.enclusiv.ui.screens.NewArticleActivity
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.launch


abstract class Util {
    companion object {
        operator fun Regex.contains(text: CharSequence): Boolean = this.matches(text)

        class WriteArticleContract: ActivityResultContract<Unit, Pair<Long, EditedArticle>?>() {
            override fun createIntent(context: Context, input: Unit): Intent {
                return Intent(context, NewArticleActivity::class.java)
            }

            override fun parseResult(resultCode: Int, intent: Intent?): Pair<Long,EditedArticle>? {
                return if (resultCode == Activity.RESULT_OK) {
                    val ret = intent?.getSerializableExtra("UPLOADED_ARTICLE") as? Pair<Long, EditedArticle>
                    Log.d("##DBG 890909", "Back With OK ${ret?.first}")
                    ret
                } else {
                    Log.d("##DBG 890909", "NULL ! ${resultCode}")
                    null
                }
            }
        }

        class ReadArticleContract: ActivityResultContract<Pair<Int, ShowedArticleProfile>, Pair<Int, UserArticleStats>?>() {
            override fun createIntent(context: Context, input: Pair<Int, ShowedArticleProfile>): Intent {
                return Intent(context, ArticleActivity::class.java).apply {
                    putExtra("ARTICLE_DATA", input)
                }
            }

            override fun parseResult(resultCode: Int, intent: Intent?): Pair<Int, UserArticleStats>? {
                return if (resultCode == Activity.RESULT_OK) {
                    intent?.getSerializableExtra("USER_ARTICLE_STATS") as? Pair<Int, UserArticleStats>
                } else {
                    null
                }
            }
        }

        fun toImageUrl(path: String, id: Long) =
            EnclusivApp.globalRemoteSourceCommunication.serverUrl.newBuilder().apply {
                addPathSegments(path)
                addQueryParameter("id", id.toString())
            }.build().toString()

        fun isEmailAddress(string: String): Boolean =
            string.isNotEmpty() && Patterns.EMAIL_ADDRESS.matcher(string).matches()

        fun <S>requestWithDeferredResultAsync(coroutineScope: CoroutineScope, request: suspend () -> S) : Deferred<S> {
            val deferred = CompletableDeferred<S>()
            coroutineScope.launch {
                try {
                    val result = request()
                    deferred.complete(result)
                } catch (e: Exception) {
                    deferred.completeExceptionally(e)
                }
            }
            return deferred
        }
    }
}